//
//  YYEHotCourseViewController.m
//  YuYingEDU
//
//  Created by 胡波 on 2018/1/4.
//  Copyright © 2018年 胡波. All rights reserved.
//

#import "YYEHotCourseViewController.h"
#import "YYEHotCourseCollectionViewCell.h"
#import "YYECoursePlayViewController.h"
#import "YYECourseNoBuyViewController.h"

@interface YYEHotCourseViewController () <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic,strong) NSMutableArray *datas;
@property (nonatomic,assign) NSInteger page;

@end

@implementation YYEHotCourseViewController



static NSString *hotCourseCell = @"courseCell";

- (NSMutableArray *)datas {
    
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"热门单课推荐";
    
    [self configUI];
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    //    [header beginRefreshing];
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    self.collectionView.mj_header = header;
    
    
    // 上拉刷新
    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [self loadMoreData];
    }];
    
    [self loadNewData];
    
}

- (void)loadNewData {
    
    self.page = 1;
    [self loadDataWithPage:self.page];
    
}

- (void)loadMoreData {
    
    self.page++;
    [self loadDataWithPage:self.page];
    
}


- (void)loadDataWithPage:(NSInteger)page {
    
    NSDictionary *dic = @{
                          @"p":@(page),
                          @"size":@10,
//                          @"token":[LoginModel shareLogin].token,
//                          @"uid":[LoginModel shareLogin].uid,
//                          @"username":[LoginModel shareLogin].username
                          };
    [SVProgressHUD show];
    [AFNHelper post:MoreHot parameters:dic success:^(id responseObject) {
        
        if (self.page == 1) {
            [self.datas removeAllObjects];
        }
        
        if ([responseObject[@"status"] isEqual:@1]) {
            
            NSArray *arr = [YYEHomeCourseModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            [self.datas addObjectsFromArray:arr];
            [self.collectionView reloadData];
            
        }
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];
        
        NSInteger totalPage = [responseObject[@"total_page"] integerValue];
        
        if (totalPage == self.page) {
            [self.collectionView.mj_footer endRefreshingWithNoMoreData];
        }
        //        [SVProgressHUD showWithStatus:responseObject[@"message"]];
        [SVProgressHUD dismissWithDelay:0.5];
        
    } faliure:^(id error) {
        
        NSLog(@"请求失败");
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];
        [SVProgressHUD showWithStatus:ShowError];
        [SVProgressHUD dismissWithDelay:0.5];
        
    }];
    
}

- (void)configUI {
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[YYEHotCourseCollectionViewCell class] forCellWithReuseIdentifier:hotCourseCell];
 
    [self.collectionView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(UIEdgeInsetsMake(0, 3, 0, 3));
    }];
    
}

#pragma mark -- UIcollectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.datas.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    YYEHotCourseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:hotCourseCell forIndexPath:indexPath];
    cell.model = self.datas[indexPath.item];
    return cell;
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(SCREEN_WIDTH / 2 - 3, 71 + (SCREEN_WIDTH / 2 - 6) * 0.55);
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    YYEHomeCourseModel *model = self.datas[indexPath.row];
    

    
    NSDictionary *dic = @{
                          @"uid":[LoginModel shareLogin].uid,
                          @"username":[LoginModel shareLogin].username,
                          @"token":[LoginModel shareLogin].token,
                          @"cid":model.id
                          };
    [SVProgressHUD show];
    [AFNHelper post:LessonAuthority parameters:dic success:^(id responseObject) {
        
        //        NSLog(@"%@",responseObject);
        
        NSString *isBuy = responseObject[@"data"][@"is_buy"];
        NSString *isCollect = [responseObject[@"data"][@"is_collect"] stringValue];
        NSString *mid = responseObject[@"data"][@"mid"];
        NSString *playback = responseObject[@"data"][@"playback"];
        NSString *mid2 = responseObject[@"data"][@"mid2"];
        NSString *playback2 = responseObject[@"data"][@"playback2"];
        
        YYECoursePlayViewController *vc = [[YYECoursePlayViewController alloc]init];
        vc.cid = model.id;
        vc.isBuy = isBuy;
        vc.isCollect = isCollect;
        vc.mid = mid;
        vc.playback = playback;
        vc.mid2 = mid2;
        vc.playback2 = playback2;
        
        [self.navigationController pushViewController:vc animated:YES];
        [SVProgressHUD showWithStatus:responseObject[@"message"]];
        [SVProgressHUD dismiss];
        
    } faliure:^(id error) {
        
        NSLog(@"请求失败");
        [SVProgressHUD dismiss];
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
